#!/bin/sh
#
#    Copyright (C) 2009  Vitaly Druzhinin
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    On Debian systems, the complete text of the GNU General Public
#    License can be found in /usr/share/doc/flex-fw/GPL-3 file.
#
#    On Slackware systems, the complete text of the GNU General Public
#    License can be found in /usr/doc/flex-fw/GPL-3 file.
#
USERID=`id -u`
if test ! "$USERID" = "0"
then
    echo "Unsufficient privileges: you must became a root..."
    exit 1
fi


echo "Try to make the Slackware package"

SRCDIR="./debian"
PKGNUM=1dve
VER=""
CURDIR=`pwd`

echo -n "Step 1: Get package version: "
if test -z "$VER"
then
    VER=`grep "^Version: \([0-9]\+\.\)\{2\}[0-9]\+" ./$SRCDIR/DEBIAN/control | cut -d " " -f 2`
    if test -z "$VER"
    then
        echo "Fail! Invalid package version from ./debian/DEBIAN/control!"
        exit 1
    fi
fi
echo "Done. Version $VER."


PKGNAME=flex-fw-$VER-noarch-$PKGNUM.tgz


echo -n "Step 2. Make temporary directory: "
TMPDIR=`mktemp -d`
if test $? -gt 0
then
    echo "Fail! Error creating temporary directory!"
    exit 1
fi

echo "Done. $TMPDIR created"


echo -n "Step 3. Copy source tree $SRCDIR to $TMPDIR: "
cp -ax $SRCDIR $TMPDIR
if test $? -gt 0
then
    echo "Fail! Check access rights or disk free space!"
    exit 1
fi
echo "Done."


echo -n "Step 4. Remove .svn: "
find $TMPDIR/debian -name '.*' -exec rm -rf {} \; >/dev/null 2>&1
echo "Done."


echo -n "Step 5. Remove backups: "
find $TMPDIR/debian -name '*~' -exec rm -rf {} \; >/dev/null 2>&1
echo "Done."


echo "Step 6. Replace /bin/dash to /bin./ash:"
for FNAME in `grep -d recurse -l "/bin/dash" $TMPDIR/debian/*`
do
    echo -n " - process $FNAME: "
    sed -i "1 s/\/bin\/dash/\/bin\/ash/" $FNAME
    echo "Done."
done
echo "Done."

echo -n "Step 7. Set current version to $VER: "
sed -i "s/X\.X\.X/$VER/" $TMPDIR/debian/etc/flex-fw/vars/base/version
echo "Done."

echo -n "Step 8. Rename etc/init.d/flex-fw to etc/rc.d/rc.flex-fw: "
mkdir -p $TMPDIR/debian/etc/rc.d
mv $TMPDIR/debian/etc/init.d/flex-fw $TMPDIR/debian/etc/rc.d/rc.flex-fw
rm -rf $TMPDIR/debian/etc/init.d
echo "Done."


echo "Step 9: Make Slackware files: "
echo -n " - install/doinst.sh: "
mkdir -p $TMPDIR/debian/install
cp $TMPDIR/debian/DEBIAN/postinst $TMPDIR/debian/install/doinst.sh
sed -i "/^if test \"\$1\" = \"configure\"/,/^fi$/ c configure" $TMPDIR/debian/install/doinst.sh
sed -i "s/\/usr\/share\/doc\/flex-fw/\/usr\/doc\/flex-fw/" $TMPDIR/debian/install/doinst.sh
chmod 0644 $TMPDIR/debian/install/doinst.sh
echo "Done."

echo -n " - install/predelete.sh: "
cp $TMPDIR/debian/DEBIAN/prerm $TMPDIR/debian/install/predelete.sh
chmod 0644 $TMPDIR/debian/install/predelete.sh
echo "Done."

echo -n " - install/slack-required: "
echo ash > $TMPDIR/debian/install/slack-required
echo iptables >> $TMPDIR/debian/install/slack-required
echo iptables >> $TMPDIR/debian/install/slack-required
echo conntrack-tools >> $TMPDIR/debian/install/slack-required
echo "Done."

echo -n " - install/slack-desc: "
cat <<END_OF_TEXT >$TMPDIR/debian/install/slack-desc
flex-fw: flex-fw-$VER
flex-fw:
flex-fw: flex-fw is a front-end application for iptables utility.
flex-fw: Required packages: ash, iptables, conntrack-tools
flex-fw:
flex-fw: Project page: http://flex-fw.net/
flex-fw: Copyright (c) 2009-2014  Vitaly Druzhinin.
flex-fw:
flex-fw:
END_OF_TEXT
echo "Done."

echo -n " - remove DEBIAN: "
rm -rf $TMPDIR/debian/DEBIAN
echo "Done."

echo -n " - rename usr/share/doc/flex-fw to usr/doc/flex-fw: "
mkdir -p $TMPDIR/debian/usr/doc/flex-fw
mv $TMPDIR/debian/usr/share/doc/flex-fw/* $TMPDIR/debian/usr/doc/flex-fw
rm -rf $TMPDIR/debian/usr/share/doc/flex-fw
rm -rf $TMPDIR/debian/usr/share/doc
rm -rf $TMPDIR/debian/usr/share
echo "Done."


echo -n "Step 10. Set permissions to root:root: "
chown -R root:root $TMPDIR/debian
echo "Done."


echo -n "Step 11. Make package $PKGNAME: "
cd $TMPDIR/debian
tar czf $PKGNAME ./*
cd $CURDIR
cp $TMPDIR/debian/$PKGNAME .
echo "Done."


echo -n "Step 12. Remove $TMPDIR: "
rm -rf $TMPDIR
echo "Done."
